//
//  MCShareBaseShareWayView.swift
//  StoreManage
//
//  Created by MC on 2018/2/1.
//  Copyright © 2018年 MC. All rights reserved.
//

import UIKit


class MCShareBaseShareWayView: UIView {

    var saveView : UIView?
        
    var superView : UIView?

    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.backgroundColor = UIColor.init(white: 0, alpha: 0.4)
        let tap = UITapGestureRecognizer.init(target: self, action: #selector(dismissSelf))
        self.addGestureRecognizer(tap)
        
        
        
        self.addSubview(bgView)
        bgView.snp.remakeConstraints { (make) ->Void in
            make.left.right.bottom.equalTo(self)
            make.height.equalTo(150)
        }
        
        bgView.addSubview(lineView)
        lineView.snp.remakeConstraints { (make) ->Void in
            make.left.right.equalTo(bgView)
            make.height.equalTo(1)
            make.top.equalTo(bgView).offset(20)
        }

        
        bgView.addSubview(promptLabel)
        promptLabel.snp.remakeConstraints { (make) ->Void in
            make.center.equalTo(lineView)
            make.size.equalTo(CGSize.init(width: 50, height: 14))
        }
        
        
        let buttonWH : CGFloat = 69
        let margin = (MCScreenWidth - buttonWH*3) / 4
        
        bgView.addSubview(friendButton)
        friendButton.snp.remakeConstraints { (make) ->Void in
            make.left.equalTo(margin)
            make.size.equalTo(CGSize.init(width: buttonWH, height: buttonWH))
            make.centerY.equalTo(bgView.snp.centerY).offset(10)
        }
        
        bgView.addSubview(friendsterButton)
        friendsterButton.snp.remakeConstraints { (make) ->Void in
            make.centerX.equalTo(bgView)
            make.size.equalTo(CGSize.init(width: buttonWH, height: buttonWH))
            make.centerY.equalTo(friendButton)
        }

        bgView.addSubview(saveButton)
        saveButton.snp.remakeConstraints { (make) ->Void in
            make.right.equalTo(-margin)
            make.size.equalTo(CGSize.init(width: buttonWH, height: buttonWH))
            make.centerY.equalTo(friendButton)
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    @objc func dismissSelf() {
        self.removeFromSuperview()
    }
    
    func createImage(view:UIView) -> UIImage {
        let image = MCImage_createdWithView(view: view)
        let data = UIImageJPEGRepresentation(image, 0.2)
        return UIImage.init(data: data!) ?? image
        
    }
    
    @objc func wxFriendButtonClicked() {
        
        dismissSelf()
        
        let message = WXMediaMessage.init()
    
        //发送的图片
        let image = createImage(view: saveView!)
        let imageObject =  WXImageObject()
        imageObject.imageData = UIImagePNGRepresentation(image)
        message.mediaObject = imageObject
        
        //图片缩略图
        let width = 240.0 as CGFloat
        let height = width*image.size.height/image.size.width
        
        UIGraphicsBeginImageContext(CGSize(width: width, height: height))
        image.draw(in: CGRect(x: 0, y: 0, width: width, height: height))
        message.setThumbImage(UIGraphicsGetImageFromCurrentImageContext())
        UIGraphicsEndImageContext()
        
        
        let req =  SendMessageToWXReq()
        req.bText = false
        req.message = message
        req.scene = Int32(WXSceneSession.rawValue)
        WXApi.send(req)
    }
    
    @objc func wxCycleButtonClicked() {
        
        dismissSelf()

        let message = WXMediaMessage.init()
        
        //发送的图片
        let image = createImage(view: saveView!)
        let imageObject =  WXImageObject()
        imageObject.imageData = UIImagePNGRepresentation(image)
        message.mediaObject = imageObject
        
        //图片缩略图
        let width = 240.0 as CGFloat
        let height = width*image.size.height/image.size.width
        
        UIGraphicsBeginImageContext(CGSize(width: width, height: height))
        image.draw(in: CGRect(x: 0, y: 0, width: width, height: height))
        message.setThumbImage(UIGraphicsGetImageFromCurrentImageContext())
        UIGraphicsEndImageContext()
        
        
        let req =  SendMessageToWXReq()
        req.bText = false
        req.message = message
        req.scene = Int32(WXSceneTimeline.rawValue)
        WXApi.send(req)

    }

    
    @objc func saveButtonClicked() {
        
        dismissSelf()
        
        let image = MCImage_createdWithView(view: saveView!)
        
        UIImageWriteToSavedPhotosAlbum(image, self, #selector(savedPhotosAlbum(image:didFinishSavingWithError:contextInfo:)), nil)
    }
    @objc func savedPhotosAlbum(image: UIImage, didFinishSavingWithError error: NSError?, contextInfo: AnyObject) {
        
        if error != nil {
            MCHUD_text(message: "保存失败", view: superView, delay: 1.2)
        } else {
            MCHUD_text(message: "已保存至相册", view: superView, delay: 1.2)
        }
    }


    private lazy var bgView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor.white
        return view
    }()

    
    private lazy var lineView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor.MCGray_light
        return view
    }()
    
    lazy var promptLabel: UILabel = {
        let label = UILabel()
        label.font = MCFont(12)
        label.textAlignment = NSTextAlignment.center
        label.text = "分享至"
        label.textColor = UIColor.MCGray_middle
        return label
    }()
    
    lazy var friendButton: UIButton = {
        let button = UIButton.init(type: UIButtonType.custom)
        button.setImage(#imageLiteral(resourceName: "Share_WX_Friend"), for: UIControlState.normal)
        button.addTarget(self, action: #selector(wxFriendButtonClicked), for: UIControlEvents.touchUpInside)
        return button
    }()

    lazy var friendsterButton: UIButton = {
        let button = UIButton.init(type: UIButtonType.custom)
        button.setImage(#imageLiteral(resourceName: "Share_WX_Friendster"), for: UIControlState.normal)
        button.addTarget(self, action: #selector(wxCycleButtonClicked), for: UIControlEvents.touchUpInside)
        return button
    }()
    
    lazy var saveButton: UIButton = {
        let button = UIButton.init(type: UIButtonType.custom)
        button.setImage(#imageLiteral(resourceName: "Share_save"), for: UIControlState.normal)
        button.addTarget(self, action: #selector(saveButtonClicked), for: UIControlEvents.touchUpInside)
        return button
    }()
}
